package com.zhong.beautifulimageapp.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v4.graphics.drawable.RoundedBitmapDrawable;
import android.support.v4.graphics.drawable.RoundedBitmapDrawableFactory;
import android.support.v7.widget.CardView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.BitmapImageViewTarget;
import com.zhong.beautifulimageapp.R;
import com.zhong.beautifulimageapp.model.EventBusMessageAboutTopicID;
import com.zhong.beautifulimageapp.model.TopicBriefItem;
import com.zhong.beautifulimageapp.model.UserDetailInfo;
import com.zhong.beautifulimageapp.utils.DateTimeDistance;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 我的信息中三个fragment中list的适配器
 * Created by 俊峰 on 2017/7/27.
 */

public class MyInfoListAdapter extends BaseAdapter {

    private List<TopicBriefItem> mItems;
    private List<UserDetailInfo.DataBean.RecentRepliesBean> mRepliesBeen;
    private List<UserDetailInfo.DataBean.RecentTopicsBean> mTopicsBeen;
    private LayoutInflater mInflater;

    public MyInfoListAdapter(Context context, List<TopicBriefItem> briefItems, List<UserDetailInfo.DataBean.RecentTopicsBean> recentTopicsBeen, List<UserDetailInfo.DataBean.RecentRepliesBean> recentRepliesBeen) {
        mItems = briefItems;
        mRepliesBeen = recentRepliesBeen;
        mTopicsBeen = recentTopicsBeen;
        mInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        int result = 0;
        if (mItems != null) {
            result = mItems.size();
        }
        if (mRepliesBeen != null) {
            result = mRepliesBeen.size();
        }
        if (mTopicsBeen != null) {
            result = mTopicsBeen.size();
        }
        return result;
    }

    @Override
    public Object getItem(int position) {
        Object o = null;
        if (mItems != null) {
            o = mItems.get(position);
        }
        if (mRepliesBeen != null) {
            o = mRepliesBeen.get(position);
        }
        if (mTopicsBeen != null) {
            o = mTopicsBeen.get(position);
        }
        return o;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view;
        if (convertView != null) {
            view = convertView;
        } else {
            view = mInflater.inflate(R.layout.activity_my_list_item, parent, false);
            ViewHolder holder = (ViewHolder) view.getTag();
            if (holder == null) {
                holder = new ViewHolder(view);
                view.setTag(holder);
            }
            bindData(holder, position);
        }
        return view;
    }

    private void bindData(final ViewHolder holder, int position) {
        if (mItems != null) {
            TopicBriefItem item = mItems.get(position);
            setDataDetail(holder, item.getAuthor().getAvatar_url(), item.getTitle(), item.getCreate_at(), item.getAuthor().getLoginname(), item.getId());
        }
        if (mRepliesBeen != null) {
            UserDetailInfo.DataBean.RecentRepliesBean repliesBean = mRepliesBeen.get(position);
            setDataDetail(holder, repliesBean.getAuthor().getAvatar_url(), repliesBean.getTitle(), repliesBean.getLast_reply_at(), repliesBean.getAuthor().getLoginname(), repliesBean.getId());
        }
        if (mTopicsBeen != null) {
            UserDetailInfo.DataBean.RecentTopicsBean topicsBean = mTopicsBeen.get(position);
            setDataDetail(holder, topicsBean.getAuthor().getAvatar_url(), topicsBean.getTitle(), topicsBean.getLast_reply_at(), topicsBean.getAuthor().getLoginname(), topicsBean.getId());
        }
    }

    private void setDataDetail(final ViewHolder holder, String iconUrl, String title, String time, final String authorName, final String id) {
        Glide.with(holder.mMyListItemUserIcon.getContext())
                .load(iconUrl)
                .asBitmap()
                .placeholder(R.mipmap.app)
                .centerCrop()
                .into(new BitmapImageViewTarget(holder.mMyListItemUserIcon){
                    @Override
                    protected void setResource(Bitmap resource) {
                        RoundedBitmapDrawable drawable = RoundedBitmapDrawableFactory.create(holder.mMyListItemUserIcon.getResources(), resource);
                        drawable.setCircular(true);
                        holder.mMyListItemUserIcon.setImageDrawable(drawable);
                    }
                });
        holder.mMyListItemUserIcon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EventBus.getDefault().post(authorName);
            }
        });
        holder.mMyListItemTitle.setText(title);
        holder.mMyListItemUserName.setText(authorName);
        holder.mMyListItemTime.setText(DateTimeDistance.getTimeDistance(time, 2));
        holder.mMyListItemAll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EventBusMessageAboutTopicID topicId = new EventBusMessageAboutTopicID();
                topicId.setId(id);
                EventBus.getDefault().post(topicId);
            }
        });
    }

    class ViewHolder {
        @BindView(R.id.my_list_item_userIcon)
        ImageView mMyListItemUserIcon;
        @BindView(R.id.my_list_item_title)
        TextView mMyListItemTitle;
        @BindView(R.id.my_list_item_userName)
        TextView mMyListItemUserName;
        @BindView(R.id.my_list_item_time)
        TextView mMyListItemTime;
        @BindView(R.id.my_list_item_all)
        CardView mMyListItemAll;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
